import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;

/**
 * Created by rudysback on 2/6/14.
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Easiest exercise ever");
        printHorizontalLine(1);
        System.out.println("Draw a Horizontal line");
        printHorizontalLine(8);
        System.out.println("Draw a Vertical Line");
        printVerticalLine(3);
        System.out.println("Draw a Right Triangle");
        printRightTriangle(3);
        System.out.println("Draw an Isosceles Triangle");
        printIsoscelesTriangle(3);
        System.out.println("Draw a Diamond");
        printDiamond(3);
        System.out.println("Draw a Diamond with Name");
        printNameDiamond(3);
        System.out.println("FizzBuzz Exercise");
        fizzBuzz();
        System.out.println("Prime Factors Exercise");
        System.out.println(generate(7));
        System.out.println(generate(30));
    }

    public static TreeSet<Integer> generate(int n) {
        TreeSet<Integer> primes = new TreeSet<Integer>();
        primes.addAll(findPrimeFactors(n));

        if (primes.size() == 1 && primes.contains(n)) {
            return new TreeSet<Integer>();
        } else {
            return primes;
        }
    }

    public static ArrayList findPrimeFactors(int n) {
        ArrayList<Integer> prime = new ArrayList<Integer>();
        int maxLower = (int) Math.sqrt(n);
        for(int i=2; i <= maxLower; i ++){
            if (n%i == 0) {
                prime.addAll(findPrimeFactors(n / i));
                prime.addAll(findPrimeFactors(i));
                return prime;
            }
        }
        prime.add(n);
        return prime;
    }

    public static void fizzBuzz() {
        String numberString = "";
        for(int i = 1; i <= 100; i++) {
            numberString = "";
            if (i % 3 == 0 || i % 5 == 0) {
                if (i % 3 == 0) {numberString += "Fizz";}
                if (i % 5 == 0) {numberString += "Buzz";}
            }else {
               numberString += i;
            }
            System.out.println(numberString);
        }
    }


    public static void printHorizontalLine(int n) {
        String printString = "";
        for(int i = 1; i <= n; i++) {
            printString += "*";
        }
        System.out.println(printString);
    }

    public static void printVerticalLine(int n) {
        for(int i = 1; i <= n; i++) {
            System.out.println("*");
        }
    }

    public static void printRightTriangle(int n) {
        for(int i = 1; i <= n; i++) {
            printHorizontalLine(i);
        }
    }

    public static void printNameDiamond(int n){
        printIsoscelesTriangle(n-1, 1);
        System.out.println("Anirudh");
        printReverseIsoscelesTriangle(n-1);
    }

    public static void printDiamond(int n) {
        printIsoscelesTriangle(n);
        printReverseIsoscelesTriangle(n-1);
    }


    public static void printIsoscelesTriangle(int n) {
        printIsoscelesTriangle(n, 0);
    }

    public static void printIsoscelesTriangle(int n, int leftOffset) {
        for(int i = 1; i <= n; i++) {
            printLineForIsoscelesTriangle(nthOddNumber(i), nthOddNumber(n + leftOffset));
        }
    }


    private static void printReverseIsoscelesTriangle(int n) {
        for(int i = n; i >= 1; i--) {
            printLineForIsoscelesTriangle(nthOddNumber(i), nthOddNumber(n+1));
        }
    }

    private static void printLineForIsoscelesTriangle(int currentWidth, int maxWidth) {
        printSpaces((maxWidth - currentWidth)/2);
        printHorizontalLine(currentWidth);
    }
    private static void printSpaces(int n) {
        String spaces = "";
        for(int i=1; i<=n; i++) {
            spaces += " ";
        }
        System.out.print(spaces);
    }

    public static int nthOddNumber(int n) {
        return 2*n-1;
    }

}
